package com.smartfast4j.manager.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 允许跨域访问配置处理
 */
@Configuration
public class CorsConf {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        // 设置跨域缓存实践为30分钟
        corsConfiguration.setMaxAge(1800L);//跨域和请求两次问题的解决(通过设置Access-Control-Max-Age来控制浏览器在多长时间内（单位s）无需在请求时发送预检(OPTIONS)请求)
        corsConfiguration.addExposedHeader("loginStatus");//添加自定义输出请求头
        corsConfiguration.addExposedHeader("permissionLimit");//添加自定义输出请求头
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }

}